package com.xky.controller;

import com.xky.pojo.Book;
import com.xky.pojo.PageBean;
import com.xky.pojo.Result;
import com.xky.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 图书类
 */
@RestController
@RequestMapping("/book")
@Slf4j//日志
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 添加图书信息
     * @param book
     * @return
     */
    @PostMapping("/add")
    public Result addBooks(@RequestBody Book book){
        log.info("添加图书信息{}",book);
        bookService.addBooks(book);
        return Result.success();
    }

    /**
     * 删除图书信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteBookById(Integer id){
        log.info("删除图书信息,id为{}",id);
        bookService.deleteById(id);
        return Result.success();
    }

    /**
     * 删除图书编号为？的信息
     * @param bookCode
     * @return
     */
    @DeleteMapping("/bookCode")
    public Result deleteByCode(@RequestParam("bookCode") Integer bookCode){
        try {
            log.info("删除编号为{}的图书", bookCode);
            bookService.deleteByCode(bookCode);
            return Result.success();
        } catch (Exception e) {
            log.error("删除图书失败，编号：{}，错误：{}", bookCode, e.getMessage());
            return Result.error("删除图书失败：" + e.getMessage());
        }
    }
    /**
     * 批量删除图书信息
     * @param id
     * @return
     * 使用 @RequestParam 明确告诉 Spring 如何处理参数
     */
    @DeleteMapping("/deleteselect")
    public Result deleteSelect(@RequestParam("id") List<Integer> id){
        log.info("批量删除图书信息数量有{}",id);
        bookService.deleteSelect(id);
        return Result.success();
    }

    /**
     * 更新图书信息
     * @param book
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Book book){
        log.info("更新图书信息{}",book);
        bookService.updateById(book);
        return Result.success();
    }

    /**
     * 根据id查询图书信息
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public Result selectById(Integer id){
      log.info("根据id查询图书信息{}",id);
      Book book = bookService.selectById(id);
      return  Result.success(book);
    }

    /**
     * 查询全部的图书信息
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(){
        log.info("查询全部的图书信息");
      List<Book> books = bookService.selectAll();
      return Result.success(books);
    }

    /**
     * 分页查询
     * @param bookName
     * @param bookLocation
     * @param bookType
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/pages")
    public Result getPage(@RequestParam(required = false) String bookName,
                         @RequestParam(required = false) String bookLocation,
                         @RequestParam(required = false) String bookType,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("分页查询：模糊书名{},书的位置{},书的种类{},起始页码{},结束页码{}",bookName,bookLocation,bookType,page,pageSize);
        PageBean select = bookService.select(bookName,bookLocation,bookType, page, pageSize);
        log.info("查询结果：总记录数：{}, 返回数据条数：{}", select.getTotal(), select.getRows() != null ? select.getRows().size() : 0);
        return Result.success(select);
    }
}
